<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'shop:order:soMain:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOrderSoMainForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { SoMain, soMainSave, soMainForm } from '/@/api/shop/order/soMain';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('shop.order.soMain');
  const { showMessage } = useMessage();
  const record = ref<SoMain>({} as SoMain);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增销售订单') : t('编辑销售订单'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('单据日期'),
      field: 'ddate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      required: true,
    },
    {
      label: t('业务类型'),
      field: 'cbustype',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('销售类型'),
      field: 'cstcode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('客户编码'),
      field: 'cusCode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('部门'),
      field: 'officeCode',
      component: 'Input',
      componentProps: {
        maxlength: 64,
      },
    },
    {
      label: t('业务员'),
      field: 'personCode',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('币种'),
      field: 'cexchName',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('汇率'),
      field: 'iexchrate',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('税率'),
      field: 'itaxrate',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('退货标志'),
      field: 'backFlag',
      component: 'Input',
      componentProps: {
        maxlength: 1,
      },
    },
    {
      label: t('单据模板'),
      field: 'ivtid',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('备注信息'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('自定义项1'),
      field: 'cdefine1',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('自定义项2'),
      field: 'cdefine2',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('自定义项3'),
      field: 'cdefine3',
      component: 'Input',
      componentProps: {
        maxlength: 20,
      },
    },
    {
      label: t('自定义项4'),
      field: 'cdefine4',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('自定义项5'),
      field: 'cdefine5',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('自定义项6'),
      field: 'cdefine6',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('自定义项7'),
      field: 'cdefine7',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('自定义项8'),
      field: 'cdefine8',
      component: 'Input',
      componentProps: {
        maxlength: 4,
      },
    },
    {
      label: t('自定义项9'),
      field: 'cdefine9',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
    },
    {
      label: t('自定义项10'),
      field: 'cdefine10',
      component: 'Input',
      componentProps: {
        maxlength: 60,
      },
    },
    {
      label: t('自定义项11'),
      field: 'cdefine11',
      component: 'Input',
      componentProps: {
        maxlength: 120,
      },
    },
    {
      label: t('自定义项12'),
      field: 'cdefine12',
      component: 'Input',
      componentProps: {
        maxlength: 120,
      },
    },
    {
      label: t('自定义项13'),
      field: 'cdefine13',
      component: 'Input',
      componentProps: {
        maxlength: 120,
      },
    },
    {
      label: t('自定义项14'),
      field: 'cdefine14',
      component: 'Input',
      componentProps: {
        maxlength: 120,
      },
    },
    {
      label: t('自定义项15'),
      field: 'cdefine15',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('自定义项16'),
      field: 'cdefine16',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('子公司店面属性'),
      field: 'chdefine1',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('战区分类'),
      field: 'chdefine2',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('合作方式'),
      field: 'chdefine3',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('市级代理'),
      field: 'chdefine4',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('区级代理'),
      field: 'chdefine5',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('招商类别'),
      field: 'chdefine6',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('招商主体'),
      field: 'chdefine7',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('运营主管'),
      field: 'chdefine8',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('省区经理'),
      field: 'chdefine9',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('市场总监'),
      field: 'chdefine10',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('战区总经理'),
      field: 'chdefine11',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('招商总监'),
      field: 'chdefine12',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('招商经理'),
      field: 'chdefine13',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('物流公司'),
      field: 'chdefine14',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('经销商收货地址'),
      field: 'chdefine15',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('经销商名称'),
      field: 'chdefine16',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('经销商简称'),
      field: 'chdefine17',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('收货联系人'),
      field: 'chdefine18',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('经销商返利余额'),
      field: 'chdefine19',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('经销商余额'),
      field: 'chdefine20',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('核发店面简称'),
      field: 'chdefine21',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('收货人联系电话'),
      field: 'chdefine22',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('经销商赠送余额'),
      field: 'chdefine23',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('是否包邮'),
      field: 'chdefine24',
      component: 'Select',
      componentProps: {
        dictType: 'sys_yes_no',
        allowClear: true,
      },
    },
    {
      label: t('包邮关联单号'),
      field: 'chdefine25',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('是否发货'),
      field: 'chdefine26',
      component: 'Select',
      componentProps: {
        dictType: 'sys_yes_no',
        allowClear: true,
      },
    },
    {
      label: t('实际控股人'),
      field: 'chdefine27',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('法人'),
      field: 'chdefine28',
      component: 'Input',
      componentProps: {
        maxlength: 80,
      },
    },
    {
      label: t('经销商首发日期'),
      field: 'chdefine29',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('核发店面归属'),
      field: 'chdefine30',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
    },
    {
      label: t('物流对接单位'),
      field: 'chdefine31',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('核发店面首发日期'),
      field: 'chdefine32',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
    },
    {
      label: t('子公司名称'),
      field: 'chdefine33',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
    },
    {
      label: t('客户返利余额'),
      field: 'chdefine34',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('客户促销返利余额'),
      field: 'chdefine35',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('可使用返利上限'),
      field: 'chdefine36',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [{ pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') }],
    },
    {
      label: t('扩展自定义项37'),
      field: 'chdefine37',
      component: 'Input',
      componentProps: {
        maxlength: 130,
      },
    },
    {
      label: t('扩展自定义项38'),
      field: 'chdefine38',
      component: 'Input',
      componentProps: {
        maxlength: 130,
      },
    },
    {
      label: t('原出库单号'),
      field: 'chdefine39',
      component: 'Input',
      componentProps: {
        maxlength: 130,
      },
    },
    {
      label: t('所属省级运营中心'),
      field: 'chdefine40',
      component: 'Input',
      componentProps: {
        maxlength: 30,
      },
    },
    {
      label: t('是否运管中心'),
      field: 'chdefine41',
      component: 'Select',
      componentProps: {
        dictType: 'sys_yes_no',
        allowClear: true,
      },
    },
    {
      label: t('是否占股100%'),
      field: 'chdefine42',
      component: 'Select',
      componentProps: {
        dictType: 'sys_yes_no',
        allowClear: true,
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await soMainForm(data);
    record.value = (res.soMain || {}) as SoMain;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        code: record.value.code,
      };
      // console.log('submit', params, data, record);
      const res = await soMainSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
